package com.hpeu_01.day0125;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
    public static void main(String[] args) {
        try {
            //创建ServerSocket
            ServerSocket ss = new ServerSocket(8080);
            //创建 Socket 连接
            Socket accept = ss.accept();
            //接收客服端的用户和密码
            InputStream is = accept.getInputStream();
            byte[] b = new byte[1024];
            int len = is.read(b);
            String username = new String(b, 0, len);

            len = is.read(b);
            String password = new String(b, 0, len);
            accept.shutdownInput();

            //处理数据
            OutputStream os = accept.getOutputStream();
            if (username !=null &&"hpeu".equals(username.trim()) && "123".equals(password.trim())){
                // 向客户端发送登录成功信息
                os.write("登录成功！".getBytes());
            }else {
                // 向客户端发送登录失败信息
                os.write("用户名或密码错误！".getBytes());
            }
            //关资源
            os.close();
            is.close();
            accept.close();
            ss.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
